package cn.online.taxi.api.driver.controller;

import cn.online.taxi.common.pojo.Result;
import cn.online.taxi.common.utils.ResultUtil;
import cn.online.taxi.manager.dto.front.Driver;
import cn.online.taxi.manager.dto.front.DriverLoginRegister;
import cn.online.taxi.sso.service.LoginService;
import cn.online.taxi.sso.service.RegisterService;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(value = "司机注册登录")
public class SsoController {

    @Reference
    private RegisterService registerService;
    @Reference
    private LoginService loginService;

    @RequestMapping(value = "/driver/register", method = RequestMethod.POST)
    @ApiOperation(value = "司机注册")
    public Result<Object> driverRegister(@RequestBody DriverLoginRegister driverLoginRegister) {
        int result = registerService.driverRegister(driverLoginRegister.getName(), driverLoginRegister.getPhone(), driverLoginRegister.getUserPwd(), driverLoginRegister.getSex());
        if(result == -1) {
            return new ResultUtil<Object>().setErrorMsg("用户名 密码 手机号不能为空");
        }else if(result == 0) {
            return new ResultUtil<Object>().setErrorMsg("手机号已被注册");
        }
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/driver/login", method = RequestMethod.POST)
    @ApiOperation(value = "司机登录")
    public Result<Driver> driverLogin(String phone, String userPwd, String driverLongitude, String driverLatitude) {
        Driver driver = loginService.driverLogin(phone, userPwd, driverLongitude, driverLatitude);
        return new ResultUtil<Driver>().setData(driver);
    }

    @RequestMapping(value = "/driver/checkLogin", method = RequestMethod.GET)
    @ApiOperation(value = "判断司机是否登录")
    public Result<Object> checkLogin(@RequestParam(defaultValue = "") String token) {
        loginService.getDriverToken(token);
        return new ResultUtil<Object>().setData(null);
    }

}
